package sgi.client;

import ocsf.client.*;
import java.io.*;

import javax.swing.JOptionPane;

import sgi.message.*;

public class SGIClientConnectionController extends AbstractClient{
	/**
	 * @param args
	 * @throws IOException 
	 */
	
	private static SGIClientConnectUI clientUI;
	private static SGIClientConnectionController connection;
	
	public static SGIClientConnectionController createConnection() throws IOException
	{
		if(connection == null)
		{
				connection = new SGIClientConnectionController();
				clientUI.setClient(connection);
		}
		
		return connection;
	}
	
	
	private SGIClientConnectionController() throws IOException  
	{
		 super(); //Call the superclass constructor
		 SGIClientConnectionController.clientUI=new SGIClientConnectUI();
		 clientUI.setVisible(true);
	}
	
	private SGIClientConnectionController(String host,int port,SGIClientConnectUI clientUI) throws IOException  
	{
		 super(host, port); //Call the superclass constructor
		 SGIClientConnectionController.clientUI=clientUI;
		 clientUI.setVisible(true);
		 try {
			 	openConnection();
		 } catch (IOException e) {clientUI.setNotification("Connection to server failed."); }
		// clientUI.setNotification("Connection to server succeeded.");
	}
	
	/*Send the server response to the generator of the message*/
	public void handleMessageFromServer(Object msg) 		
	{
		if(msg instanceof MessageLogin)
			try {
				(SGILoginController.createLogin()).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Login handling exception.");}
		else if(msg instanceof MessageRegister)
		{
			(SGIRegisterController.createRegister()).handleMessage(msg);
		}
		else if(msg instanceof MessageComplaint)
		{
			try {
				(SGIComplaintController.create(null)).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Complaint handling exception.");}
		}
		else if(msg instanceof MessageSearch)
		{
			try {
				(SGISearchArchiveController.create()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGISearchArchiveController.getUI(),"Message handling failure");	; e.printStackTrace();}
		}
		else
		{
			clientUI.setNotification("Message type unknown");
		}
		
	}
	
	public void quit()
	{
	  try
	  {
	     closeConnection();
	  }
	  catch(IOException e) {e.printStackTrace();}
	  System.exit(0);
	}
	
	  public void handleMessageFromClientUI(Message msg) throws IOException
	  {
	    try
	    {
	    	sendToServer(msg);
	    }
	    catch(IOException e)
	    {
	      clientUI.setNotification("Could not send message to server.");
	      throw e;
	    }
	  }
	  
	  public void startConnection(String host,int port) throws IOException
	  {
			this.setHost(host);
			this.setPort(port);
			 try {
				 	this.openConnection();
				 	//setNotification("Connection to server succeeded.");
				 	this.handleMessageFromClientUI(new MessagePrompt("Client connected."));
				 	clientUI.setVisible(false);
				 	SGILoginController.createLogin();
			 } catch (IOException e) {throw e;}
	  }
	  
}
